import os

import cv2
import numpy as np
import tqdm
import pandas as pd
import database


def insert_zipdataset():
    if database.mydb is None:
        database.mydb = database.MyDatabase()
    cur = database.mydb.conn.cursor()
    strcur = "INSERT INTO zipdataset (id,name, create_time) values (1, 'mnist', '2023-11-15');"
    cur.execute(strcur)
    database.mydb.conn.commit()


def insert_data(x_path, y):
    if database.mydb is None:
        database.mydb = database.MyDatabase()
    cur = database.mydb.conn.cursor()
    strcur = f"insert into data_file (filename, label, zip_id) values ('{x_path}', {y}, 1);"
    cur.execute(strcur)
    database.mydb.conn.commit()


def main():
    # insert_zipdataset()
    data = pd.read_csv("../datasets/mnist/labels.csv")
    print(len(data))
    with tqdm.tqdm(total=len(data)) as tbar:
        for row_name, row in data.iterrows():
            x_path, y = row
            tbar.update(1)
            insert_data(x_path, y)


if __name__ == "__main__":
    main()
